{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a1596f5-79ca-4d62-b392-ee8e6f0927d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8cb46ea0-f4b9-4d3a-8ea0-4ea05de061c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "arr1=np.arange(15)\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e7a8a41f-1877-4938-810c-bc7f80f851db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6]\n"
     ]
    }
   ],
   "source": [
    "arr2=arr1[2:7:2]\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef1e2b6-4dd0-4fe3-8e34-82615d526274",
   "metadata": {},
   "source": [
    "arr2=arr1[2:7:2]从arr1中切片出新数组    起点  终点  间隔步长  右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ca9751c2-e4b8-4940-8008-096b7928dc38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n"
     ]
    }
   ],
   "source": [
    "arr3=arr1.reshape(3,5)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a52e4e2-5008-48c4-9c63-df0d7a25c562",
   "metadata": {},
   "source": [
    "arr1.reshape(3,5)将1维重整reshape为3行5列的2 维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "533aa145-17b6-440d-81d1-47ea14441b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccc338ab-aa24-4234-9336-b6308bffb56d",
   "metadata": {},
   "source": [
    "访问第2行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3de38430-ce5f-4762-8516-b2b48c873b84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6  7]\n",
      " [11 12]]\n"
     ]
    }
   ],
   "source": [
    "arr4=arr3[1:3,1:3]\n",
    "print(arr4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ae133ee-a865-4e46-a5ad-945d19f389e3",
   "metadata": {},
   "source": [
    "切片  1，2行，1，2列的数据，右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2b7ca6f4-4b24-4194-bb69-58e33503fe0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(12)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[1,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84abbda1-4c15-4ce7-82cb-7b149cd8d53f",
   "metadata": {},
   "source": [
    "切片访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7d6d4fd2-4e5f-4b1f-9ca9-ce4c4352273a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  7],\n",
       "       [11,  0]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[1][1]=0\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "554ee88a-564d-4ee0-adaa-c20da8ba4c82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11,  0, 13, 14]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9de3c486-9b56-4a13-9343-6a81ae448bbf",
   "metadata": {},
   "source": [
    "改变arr3切片出来的arr4元素的值，arr3也同步变了，指针指在一个地方的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8574af55-1ffc-4f23-a411-59f1b4c28892",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  8],\n",
       "       [ 0, 13]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5=np.copy(arr3[1:3,2:4])\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b59a096d-939c-4a08-b5d1-4002f0313385",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [ 0, 13]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5[0][1]=10\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9cd12398-6c78-4fa7-8aa9-40b5c2af5d34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11,  0, 13, 14]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d6414ee-baae-4adf-b74c-2e55ed8e20cc",
   "metadata": {},
   "source": [
    "arr5=np.copy直接复制一份数据，修改不会改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ca5a3bc9-1aa3-4488-9e18-1e1dece3dd2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2],\n",
       "        [ 3,  4,  5],\n",
       "        [ 6,  7,  8],\n",
       "        [ 9, 10, 11],\n",
       "        [ 0, 13, 14]]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr6=arr3.reshape((1,5,3))\n",
    "arr6"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7361bccd-ecf4-49cf-abe4-b0d81cf41e64",
   "metadata": {},
   "source": [
    "reshape成1个5行3列的数组，只要元素总个数一致，就可以搞"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "aeac78c0-6ae0-4087-ba62-f4cd904b5aaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,  0, 13, 14])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7=arr3.reshape(15)\n",
    "arr7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "df7d9ddc-a75a-40ed-8b53-0aad2da409d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 2,  3],\n",
       "        [ 4,  5]],\n",
       "\n",
       "       [[ 6,  7],\n",
       "        [ 8,  9],\n",
       "        [10, 11]]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr8=np.arange(12)\n",
    "arr9=arr8.reshape((2,3,2))\n",
    "arr9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "105a397a-c2e6-4353-914e-377783120856",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr10=arr9.ravel()\n",
    "arr10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ce3828a-48d0-4e0a-b009-0a3a5e8061f5",
   "metadata": {},
   "source": [
    "arr9.ravel()将3维数组压平成1维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "76c8a8e5-3c07-410e-8b69-c477de69dac8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 2,  3]],\n",
       "\n",
       "       [[ 4,  5],\n",
       "        [ 6,  7]],\n",
       "\n",
       "       [[ 8,  9],\n",
       "        [10, 11]]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr11=arr8.reshape((3,2,2))\n",
    "arr11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "61997ff2-57da-452b-8a64-d909f51d7640",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr12=arr11.flatten()\n",
    "arr12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9372c774-44e9-4642-addf-bc254dc78005",
   "metadata": {},
   "source": [
    "arr11.flatten()也是拉成1维数组操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "bc19c35d-10cb-40e6-95bb-8c569dd94b7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['1', '2', '3', 'a', 'b', 'c', 'd'],\n",
       "       ['4', '5', '6', 'e', 'f', 'g', 'h']], dtype='<U21')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list1=[[1,2,3],[4,5,6]]\n",
    "my_list2=[['a','b','c','d'],['e','f','g','h']]\n",
    "arr13=np.hstack([np.array(my_list1),np.array(my_list2)])\n",
    "arr13"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ead1cba7-e1b5-4e41-88ff-b9e8c76e0253",
   "metadata": {},
   "source": [
    "转换成ndarray后用hsatck进行数组按列拼接，行数一样就行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bfd30d2b-9498-46bb-9d8d-4cda19db5046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['1', '2', '3'],\n",
       "       ['4', '5', '6'],\n",
       "       ['7', '8', '9'],\n",
       "       ['a', 'b', 'c'],\n",
       "       ['e', 'f', 'g']], dtype='<U21')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list3=[[1,2,3],[4,5,6],[7,8,9]]\n",
    "my_list4=[['a','b','c'],['e','f','g']]\n",
    "arr14=np.vstack([np.array(my_list3),np.array(my_list4)])\n",
    "arr14"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9ea3624-fbf7-4dab-824a-cef6566c8e14",
   "metadata": {},
   "source": [
    "转换成ndarray后用vsatck进行数组按行拼接，列数一样就行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "be87cb48-4b21-486d-b083-dbd11827cb90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
       "       18, 19, 20, 21, 22, 23, 24])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr15=np.arange(1,25)\n",
    "arr15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "14784642-257a-411d-bffb-ca2956f6a487",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,\n",
       "       42, 43, 44, 45, 46, 47, 48])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr16=np.arange(25,49)\n",
    "arr16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "caf73283-018e-4af3-bd49-fcfa780d940f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8],\n",
       "        [ 9, 10, 11, 12]],\n",
       "\n",
       "       [[13, 14, 15, 16],\n",
       "        [17, 18, 19, 20],\n",
       "        [21, 22, 23, 24]]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr17=arr15.reshape(2,3,4)\n",
    "arr17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "efbca779-86e3-4727-9a9a-e7dd6e9f61fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[25, 26, 27, 28],\n",
       "        [29, 30, 31, 32],\n",
       "        [33, 34, 35, 36]],\n",
       "\n",
       "       [[37, 38, 39, 40],\n",
       "        [41, 42, 43, 44],\n",
       "        [45, 46, 47, 48]]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr18=arr16.reshape(2,3,4)\n",
    "arr18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "dc026116-1f0c-42d0-b95c-6f0ec7f50305",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8],\n",
       "        [ 9, 10, 11, 12]],\n",
       "\n",
       "       [[13, 14, 15, 16],\n",
       "        [17, 18, 19, 20],\n",
       "        [21, 22, 23, 24]],\n",
       "\n",
       "       [[25, 26, 27, 28],\n",
       "        [29, 30, 31, 32],\n",
       "        [33, 34, 35, 36]],\n",
       "\n",
       "       [[37, 38, 39, 40],\n",
       "        [41, 42, 43, 44],\n",
       "        [45, 46, 47, 48]]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr19=np.concatenate([arr17,arr18],axis=0)\n",
    "arr19"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5decdda-76d7-41f6-98b1-c19fbf22537c",
   "metadata": {},
   "source": [
    "arr19=np.concatenate([arr17,arr18],axis=0)  3维数组，轴axis可取0 1 2 ，对应轴维度一致就可以拼接，次函数方便控制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cc17515-dc68-46a8-ac01-c9b0c57dbca6",
   "metadata": {},
   "source": [
    "结果 4个3行4列的3维数组，axis=0,2*2=4,2维和1维的度不动，axis越小，维度越高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "13e72a3e-d7b4-47e0-9519-78c2dab8c30e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8],\n",
       "        [ 9, 10, 11, 12],\n",
       "        [25, 26, 27, 28],\n",
       "        [29, 30, 31, 32],\n",
       "        [33, 34, 35, 36]],\n",
       "\n",
       "       [[13, 14, 15, 16],\n",
       "        [17, 18, 19, 20],\n",
       "        [21, 22, 23, 24],\n",
       "        [37, 38, 39, 40],\n",
       "        [41, 42, 43, 44],\n",
       "        [45, 46, 47, 48]]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr20=np.concatenate([arr17,arr18],1)\n",
    "arr20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e58a3fb1-4b92-4650-9b50-36ac843908c5",
   "metadata": {},
   "source": [
    "结果 2个6行4列的3维数组，axis=1,2*3=6,3维和1维的度不动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "bc0ddc94-b93a-4114-ade5-4a2713865fef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3,  4, 25, 26, 27, 28],\n",
       "        [ 5,  6,  7,  8, 29, 30, 31, 32],\n",
       "        [ 9, 10, 11, 12, 33, 34, 35, 36]],\n",
       "\n",
       "       [[13, 14, 15, 16, 37, 38, 39, 40],\n",
       "        [17, 18, 19, 20, 41, 42, 43, 44],\n",
       "        [21, 22, 23, 24, 45, 46, 47, 48]]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr21=np.concatenate([arr17,arr18],2)\n",
    "arr21"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bdd0d53-8358-4f08-a523-5d7b46872e1d",
   "metadata": {},
   "source": [
    "结果 2个3行8列的3维数组，axis=2,2*4=8,3维和2维的度不动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43cb9503-7808-452c-8a5c-fe941e17aff2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
